# This code is part of the MWP System
# Copyright (c) 2012 Benjamin Lubin (blubin@bu.com) 
# Published under and subject to the GPLv2 license available at http://www.gnu.org/licenses/gpl-2.0.html

'''
Created on Jan 7, 2012
@author: bthump
'''

from backend.backend import Backend;
from workflow.task import Task;
from workflow.result import Result;
from workflow.flowData import Status;
from unique import UNIQUE;

class SellPaintingBackend(Backend):
   
    def __init__(self): 
        Backend.__init__(self, "Gallery"+UNIQUE, dashboard=True);

    def wire(self):
        self.register_result_listener("Painter", "SubmitPainting", self.submit_form_creator)
        self.register_result_listener("Curator", "Decision", self.status_form_creator)
        self.register_result_listener("Painter", "CheckStatus", self.review_form_creator)
        self.register_result_listener("ArtHist", "Location", self.DisplayCreator_complete)
        self.register_joined_listener([("ArtHist", "WriteHistory", Result), ("Curator", "Decision", Result, Status.COMPLETE)], self.HistoryCreator_predicate, self.HistoryCreator)

    def submit_form_creator(self, results):
        for result in results:
            task = Task.construct_from_result(result, "Curator", "Decision");
            self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE);

    def status_form_creator(self, results):
        for result in results:
            if result.data['Decision'] == "TRUE":
                task = Task.construct_from_result(result, "Curator", "AcceptNote")
                task = Task.construct_from_result(result, "Painter", "CheckStatus")
                task = Task.construct_from_result(result, "ArtHist", "WriteHistory");
            else:
                task = Task.construct_from_result(result, "Curator", "RejectNote")
                task = Task.construct_from_result(result, "Painter", "CheckStatus")
            self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE);

    def review_form_creator(self, results):
        for result in results:
            #task = Task.construct_from_result(result, "ArtHist", "WriteHistory");
            #self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE);

    def DisplayCreator_complete(self, results):
        for result in results:
            task = Task.construct_from_result(result, "ArtHist", "Location");
            self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE);

if __name__ == '__main__':
    backend = SellPaintingBackend();